package cn.st4rlight.util.excel.converter;

import java.util.Objects;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

/**
 * 用于easy-excel中 bool --> 是、否
 *
 * @author st4rlight <st4rlight@163.com>
 * Created on 2024-04-15
 */
public class BoolConverter implements Converter<Boolean> {

    private static final String YES = "是";
    private static final String  NO = "否";


    @Override
    public Class<?> supportJavaTypeKey() {
        return Boolean.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Boolean> context) {
        Boolean value = context.getValue();
        if (Objects.isNull(value)) {
            return new WriteCellData<>(CellDataTypeEnum.STRING, StringUtils.EMPTY);
        }

        return new WriteCellData<>(CellDataTypeEnum.STRING, BooleanUtils.isTrue(value) ? YES : NO);
    }
}
